package DataFile;

import java.nio.ByteBuffer;
import java.util.Formatter;

import ClassData.ConstantPoolClass_Info;



public class Structure_CP_ClassInfo extends Structure_CP_Constant {

	ConstantPoolClass_Info myClassInfo;
	int classAdress;

	public Structure_CP_ClassInfo(ConstantPoolClass_Info myClassInfo) {
		this.myClassInfo=myClassInfo;
		classAdress=-1;
	}

	public int getClassAdress() throws DataFileException {
		if (classAdress ==-1)
			throw new DataFileException(DataFileException.ErrorTypes.ADRESS_NOT_SET);
	
		return(classAdress);
	}

	public int getMemorySize() {
		int size;
		
		size = BYTE_WIDTH;  // Type
		size+= SHORT_WIDTH; // index
		size+= ADRESS_WIDTH;// Class Address
		return(size);	
	}

	protected String getMemoryMapAsString() {
		return "" + new Formatter().format("%s |%14s | size - %3d | name - %3d | %s\n", 
				getAdressAsHexString(baseAdress), 
				"(class info)", 
				getMemorySize(), 
				myClassInfo.GetNameIndex(), 
				"class adress : "+getAdressAsHexString(classAdress));
	}

	public int getClassNameIndex() {
		return(myClassInfo.GetNameIndex());
	}

	public void setClassAdress(int classAdress) {
		this.classAdress=classAdress;
	}

	/**
	 *  writes data into memory file
	 *  
	 *  @author Tavin
	 *  
	 *  @param fileData buffer for data 
	 */
	public void writeData(ByteBuffer fileData) {
		fileData.position(baseAdress-DataFileCreator.BASE_MEMORY_ADRESS);
		fileData.put((byte)myClassInfo.getType());
		fileData.putShort((short)myClassInfo.GetNameIndex());
		fileData.putInt(classAdress);
	}
}
